package com.allinpay.merchantweb.controller;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletInputStream;

import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.allinpay.merchantweb.common.utils.StringUtil;
import com.allinpay.merchantweb.constants.CommonConstants;
import com.allinpay.merchantweb.model.CommRoleInfo;
import com.allinpay.merchantweb.model.EmployeeInfo;
import com.allinpay.merchantweb.model.EnterpriseProdInfo;
import com.allinpay.merchantweb.model.Manager;
import com.allinpay.merchantweb.model.PageForLayui;
import com.allinpay.merchantweb.response.ResponseData;
import com.allinpay.merchantweb.service.IEmployeeInfoService;
import com.allinpay.merchantweb.service.ITradeQueryService;
import com.google.gson.Gson;

@Controller
@RequestMapping(value="/employee")
public class EmployeeController {

	@Autowired
	IEmployeeInfoService employeeInfoService;
	
	@GetMapping(value="")
	public String index() {
		return "setting/employee/employees";
	}
	
	@GetMapping(value="/addEmployee")
	public String addEmployee() {
		return "setting/employee/addEmployee";
	}
	
	@GetMapping(value="queryEmployeeInfos")
	@ResponseBody
	public Object queryEmployeeInfos(EmployeeInfo employeeInfo, HttpServletRequest httpRequest) {
		Object retObject = null;
		
		Manager manager = (Manager) httpRequest.getSession(false).getAttribute(CommonConstants.SESSIONMANAGER);
		
		
		CommRoleInfo commRoleInfo = new CommRoleInfo();
		//only for testing Code
		String sPageNum = httpRequest.getParameter("page");
		String sPageSize = httpRequest.getParameter("limit");
		String sBankCardNbr = httpRequest.getParameter("bankCardNbr");
		String sEmpName = httpRequest.getParameter("empName");
		String sAbbreviation = httpRequest.getParameter("abbreviation");
		String sCertNo = httpRequest.getParameter("certNo");
		String sMobile = httpRequest.getParameter("mobile");
		String sEmpStatus = httpRequest.getParameter("empStatus");
		if( !StringUtil.isEmpty(sPageNum) && !StringUtil.isEmpty(sPageSize)) {
			commRoleInfo.setPageNum(Integer.parseInt(sPageNum));
			commRoleInfo.setNumPerPage(Integer.parseInt(sPageSize));
		}
		
//		if( commRoleInfo == null) {
//			commRoleInfo = new CommRoleInfo();
//			commRoleInfo.setPageNum(1);
//			commRoleInfo.setNumPerPage(10);
//		}
		try {
			Map<String, Object>  reqMap = new HashMap<String, Object>();
			if ( !StringUtil.isEmpty(sPageSize) ) {
				reqMap.put("pageSize", Integer.parseInt(sPageSize));
			}
			if ( !StringUtil.isEmpty(sPageNum) ) {
				reqMap.put("pageNum", Integer.parseInt(sPageNum));
			}	
			
			
			if ( !StringUtil.isEmpty(sBankCardNbr) ) {
				reqMap.put("bankCardNbr", sBankCardNbr);
			}
			if ( !StringUtil.isEmpty(sEmpName) ) {
				reqMap.put("empName", sEmpName);
			}
			if ( !StringUtil.isEmpty(sAbbreviation) ) {
				reqMap.put("abbreviation", sAbbreviation);
			}
			if ( !StringUtil.isEmpty(sMobile) ) {
				reqMap.put("merchantCode", sMobile);
			}
			if ( !StringUtil.isEmpty(sEmpStatus) ) {
				reqMap.put("empStatus", sEmpStatus);
			}
			
			System.out.println("manager:" + new Gson().toJson(manager));
			employeeInfo.setInstanceId(manager.getInstanceId());
			ResponseData<EmployeeInfo> respBean = employeeInfoService.queryEmployeeInfoList(employeeInfo);
			
			PageForLayui page = new PageForLayui();
			page.setCode("0");
			page.setMsg("succ");
			page.setCount(respBean.getTotal());
			page.setData(respBean.getDataList());
			
			retObject = page;
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return retObject;
	}
	
	
	
	@RequestMapping(value="/addEmployeeInfo", method = RequestMethod.POST)
	@ResponseBody
	public Object addEmployeeInfo(EmployeeInfo employeeInfo, HttpServletRequest httpRequest) {
		

		Object retObject = null;
		try {

			employeeInfoService.addEmployeeInfo(employeeInfo);
			PageForLayui page = new PageForLayui();
			page.setCode("0");
			page.setMsg("succ");
			retObject =  page;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return new PageForLayui("110", e.getMessage() );
		}
		
		return retObject;
	}
	
	
	@RequestMapping(value="/updateEmployeeInfo")
	@ResponseBody
	public Object updateEmployeeInfo(EmployeeInfo employeeInfo, HttpServletRequest httpRequest) {
		

		Object retObject = null;
		try {

			employeeInfoService.updateEmployeeInfo(employeeInfo);
			PageForLayui page = new PageForLayui();
			page.setCode("0");
			page.setMsg("succ");
			retObject =  page;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return new PageForLayui("110", e.getMessage() );
		}
		
		return retObject;
	}
	
	
	
	
	
}
